<?php

namespace cf\api\user;

require_once dirname(__FILE__).'/../user.php';
require_once dirname(__FILE__).'/../mail.php';

function login($username, $password, $remember=false)
{
   if (!$username || !$password) {
      throw new \Exception('Specify username and password');
   }
   return \cf\User::login($username, $password, $remember);
}

function logout()
{
	$user = \cf\User::getLoggedIn();
	if ($user) {
		$user->logout();
	}
}

function resetPwd($email)
{
	$u = \cf\User::resetPwdByEmail($email);
	if ($u) {
		list($user, $pwd) = $u;
		return array($user->loginName(),$pwd);
	}
	return false;
}

function sendMessage($email, $subject, $txt)
{
	$email = trim($email);
	$uid = \cf\User::findByEmail($email);
	if (!$uid) {
		return false;
	}
	
	$m = new \cf\Mail($subject, $txt);
	try {
		\cf\Mail::send($email, $m);
	} catch (Exception $e) {
		return false;
	}
	return true;
}

?>